package com.mmd.manage.service.impl;

import com.mmd.common.entity.MmdModeMYC;
import com.mmd.intercepter.AdminInterceptor;
import com.mmd.manage.dao.MmdModeMYCDao;
import com.mmd.manage.service.ModeMYCService;
import com.mmd.manage.vo.ModeMYCVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service
public class ModeMYCServiceImpl implements ModeMYCService {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private MmdModeMYCDao mmdModeMYCDao;

    @Override
    public List<MmdModeMYC> getModeMYCList() {
        List<MmdModeMYC> list = new ArrayList<>();
        if ("admin".equals(AdminInterceptor.getUserLoginContext().getLevel())){
            list = mmdModeMYCDao.getMycListByLevel("admin");
        }else {
            list = mmdModeMYCDao.getMycList(AdminInterceptor.getUserLoginContext().getUid());
        }
        if (list == null || list.size() == 0){
            List<String> mycNameList = Arrays.asList("DHL","UPS","FedEx");
            for (String mycName : mycNameList){
                MmdModeMYC mmdModeMYC = new MmdModeMYC();
                mmdModeMYC.setMycName(mycName);
                mmdModeMYC.setMyc(BigDecimal.ZERO);
                mmdModeMYC.setUid(AdminInterceptor.getUserLoginContext().getUid());
                mmdModeMYC.setUserLevel(AdminInterceptor.getUserLoginContext().getLevel());
                mmdModeMYCDao.insert(mmdModeMYC);
            }
        }
        list = mmdModeMYCDao.getMycList(AdminInterceptor.getUserLoginContext().getUid());
        return list;
    }

    @Override
    public void setMYC(ModeMYCVO mycvo) {
        MmdModeMYC mmdModeMYC = new MmdModeMYC();
        mmdModeMYC.setId(mycvo.getId());
        mmdModeMYC.setMyc(mycvo.getMyc());
        mmdModeMYCDao.updateById(mmdModeMYC);
    }

    @Override
    public MmdModeMYC getMYCByMycAndUid(String level, long uid, String mycName) {

        return mmdModeMYCDao.getMYCByMycAndUid(level, uid, mycName);
    }

    @Override
    public MmdModeMYC getMYCByMycAndAdmin(String mycName) {
        return mmdModeMYCDao.getMYCByMycAndAdmin(mycName);
    }
}
